var util = require("../../../utils/util.js"),
  api = require("../../../utils/api.js"),
  app = getApp();
Page({
  data: {
    isGranted: false,
    is_technician: false,
    infoCompleteFlag: false,
    phone: "",
    name: "",
    avatar: "",
    birthday: "",
    show:false,
    phoneshow:false,
    sex: "",
    badge: "",
    articleList: [],
    configInfo: {},
    imgUrl: app.globalData.pathUrl,
    baseImgUrl: app.globalData.baseImgUrl,
    adList: [],
    transferFalg: 0,
    countNum: 0,
    status: 0,
    realname_status: 0,
    newPeopleGift: !1,
    message: !1,
    isVipShow: false,
    matchmaker_id: null,
    bangsHeight: wx.getStorageSync('navBarObj').bangsHeight,
    meat_min: 0,
    meat_other: 0,
    square_min: 0,
    square_other: 0,
    userDetail: {}
  },
  getMylike(e) {
    let type = e.currentTarget.dataset.type;
    if (type == 4) {
      wx.navigateTo({
        url: '/pages/square/mySquare',
      })
    } else {
      wx.navigateTo({
        url: '../myLike/myLike?type=' + e.currentTarget.dataset.type,
      })
    }
  },
  onChooseAvatar(e){
    const { avatarUrl } = e.detail;
    if(avatarUrl){
      wx.showLoading();
      this.headUploadImgs(avatarUrl)
    }
  },
  headUploadImgs(filePath) {
    wx.uploadFile({
      url: app.globalData.baseUrl + 'common/index/uploadImg', //仅为示例，非真实的接口地址//接口连接
      filePath: filePath,
      name: 'file',
      success: (res) => {
        if (res.data) {
          let json = JSON.parse(res.data)
          if (json.code == 1) {
            this.headImgWrite(json.data)
          } else {
            wx.showToast({
              title: '上传失败',
              icon: 'none'
            })
          }
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
      }
    })
  },
  headImgWrite(url) {
    api.default.setMemberInfo({
      headimgurl: url,
      flag: 2
    }).then(res => {
      wx.hideLoading()
      this.setData({show:true});
    })
  }, 
  bindphone() {
    this.setData({phoneshow:true});
    // api.default.setMemberInfo({
    //   headimgurl: url,
    //   flag: 2
    // }).then(res => {
    //   wx.hideLoading()
     
    // })
  },
  onClosephone() {
    this.setData({phoneshow:false});  
  },
  getphone(e){
    this.phone=e.detail.value;
  },
   
  onSubmitphone(e){
    if(!this.phone){
      return wx.showToast({
        title: '请填写手机号',
        icon: 'none'
      })
    }
    wx.showLoading();
    api.default.setMemberInfo({
        mobile:this.phone
    }).then(res => {
        if(res.code ==1){
            wx.hideLoading();
            this.setData({showphone:false});
            wx.showToast({
              title: '成功！',
              icon: 'success'
            })
            this.setData({phoneshow:false});  
            this.getUserInfo();
        }else{
            return wx.showToast({
                title: res.msg,
                icon: 'none'
              })
        }

   
    })
  },
 
  getNickName(e){
    this.nickname=e.detail.value;
  },
  onSubmit(e){
    if(!this.nickname){
      return wx.showToast({
        title: '请填写昵称',
        icon: 'none'
      })
    }
    wx.showLoading();
    api.default.setMemberInfo({
      alias_nickname:this.nickname
    }).then(res => {
      wx.hideLoading();
      this.setData({show:false});
      wx.showToast({
        title: '登录成功！',
        icon: 'success'
      })
      this.getUserInfo();
    })
  },
  bindload(e) {
    console.log(e);
  },
  binderror(e) {
    console.log(e);
  },
  onShow: function () {
    this.getUserInfo();
  },
  visiting() {
    wx.navigateTo({
      url: './visitingCard/visitingCard',
    })
  },
  async getUserInfo() {
    let {
      data
    } = await api.default.getUserInfo();
    if (data) {
      this.setData({
        avatar: data.headimgurl?(this.data.baseImgUrl+data.headimgurl):'../../../images/user_img.png',
        name: data.alias_nickname?data.alias_nickname:'未登录',
        phone:data.mobile
      })
    }
  },
  toDetails(t) {
    if (!this.data.articleList.length) {
      return wx.showToast({
        title: '正在建设中，敬请期待',
        icon: 'none'
      })
    }
    wx.navigateTo({
      url: "/pages/home/newactivity/newactivity?id=" + this.data.articleList[0].id
    });
  },
  async articleList() {
    var parmes = {
      pageIndex: 1,
      pageSize: 1,
      recommend_type: 2
    };
    let {
      data: {
        list,
        total
      }
    } = await api.default.articleList(parmes);
    let arr = Array.isArray(list) ? list : [];
    this.setData({
      articleList: arr
    })
  },
  toRemainder(a) {
    const {
      url
    } = a.currentTarget.dataset;
    wx.navigateTo({
      url: url,
    })
    // o.default.otherCount("mine", a.pagePath, "");
  },
  userAd: function () {
    var a = this;
    return new Promise(function (e) {
      o.default.getAPI("/user/userAd", null, "get", function (t) {
        a.setData({
          adList: t.data
        }), e("调用成功");
      });
    });
  },
  validNum: function () {
    var a = this;
    return new Promise(function (e) {
      var t = a;
      o.default.getAPI("/user/count", null, "get", function (a) {
        t.setData({
          badge: Number(a.data.giftTicketNum),
          transferFalg: Number(a.data.transferFalg)
        }), e("调用成功");
      });
    });
  },
  headViewClick: function () {
    let url = this.data.phone ? "personinfo/personinfo" : "authorization/authorization?fromFlag=0"
    wx.navigateTo({
      url: url
    });
  },
  getUserProfile() {
    wx.getUserProfile({
      desc: "用于完善会员资料",
      success: (a) => {
        console.log(a);
        if (a.errMsg == "getUserProfile:ok") {
          api.default.decryptData({
            encryptedData: a.encryptedData,
            iv: a.iv,
            sessionKey: wx.getStorageSync('session_key')
          }).then(unionid => {
            if (unionid.data && unionid.data != undefined) {
              wx.setStorageSync('unionid', unionid.data)
            }
          })
          wx.setStorageSync('wxuserInfo', a.userInfo)
          this.setData({
            avatar: a.userInfo.avatarUrl,
            phone: '',
            name: a.userInfo.nickname,
            infoCompleteFlag: false,
            isGranted: true
          })
        }
      },
      fail: function (a) {
        console.log(a);
      }
    });
  },
  toModules: function (a) {
    var e = a.currentTarget.dataset.url,
      t = a.currentTarget.dataset.value;
    if ("mine-voucher" != t) {
      var n = {
        jumpTo: e ? "pages/mine/" + e : ""
      };
      o.default.otherCount(t, "pages/mine/mine", JSON.stringify(n));
    }
    this.data.phone ? wx.navigateTo({
      url: e
    }) : wx.navigateTo({
      url: "authorization/authorization?fromFlag=0"
    });
  },
  toAuthDetail: function (a) {
    var e = a.currentTarget.dataset.url;
    wx.navigateTo({
      url: e
    });
  },
  toFollow: function (e) {
    var url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url
    });
  },
  toBook: function () {
    wx.showModal({
      title: "欢迎来到",
      content: "预约请致电" + this.data.configInfo.service_mobile + "\n(人工服务时间：周一至周日" + this.data.configInfo.service_time_start + "-" + this.data.configInfo.service_time_end + ")",
      confirmButtonText: "拨打",
      success: (res) => {
        res.confirm &&
          wx.makePhoneCall({
            phoneNumber: this.data.configInfo.service_mobile
          })
      }
    })
  },
  toMemberBenefits: function (a) {
    var e = a.currentTarget.dataset.jump;
    i.toImageDetail(e);
  },
  countNum: function () {
    this.setData({
      countNum: this.data.countNum + 1
    });
    var a = wx.getAccountInfoSync();
    this.data.countNum >= 15 && "wx2d966057f34311e5" == a.miniProgram.appId && wx.navigateTo({
      url: "./bindingphone/bindingphone"
    });
  },
  closeOverlay: function () {
    r.globalData.newPeopleGift = !1, this.setData({
      newPeopleGift: !1
    });
  },
  closeMsgOverlay: function () {
    r.globalData.message = !1, this.setData({
      message: !1
    });
  },
  onLoad: function (a) {
    this.setData({
      width: wx.getSystemInfoSync().windowWidth,
      height: .85 * wx.getSystemInfoSync().windowWidth
    });
  },
  smrz(e) {
    wx.navigateTo({
      url: "/pages/contributor/means/means",
    })
  },
  myzl(e) {
    let url = "/pages/mine/attestation/attestation";
    wx.navigateTo({
      url: "",
    })
  },
  // 会员
  member(e) {
    wx.navigateTo({
      url: '/pages/mine/vipCard/vipCard',
    })
  },
  onShareAppMessage: function (e) {
    return util.shareEvent(e)
  },
  setSetting() {
    wx.navigateTo({
      url: '../setting/setting',
    })
  }
});